import { IContext, IEntityLocalDataService, IParam } from '@/core';
import { ISys_deploy_system } from '../interface/i-sys-deploy-system';
import keys from '../sys-deploy-system-keys';
import { Sys_menuDTOHelp } from '@/api/sys-menu/dto-help/sys-menu-helps';
import { Sys_componentDTOHelp } from '@/api/sys-component/dto-help/sys-component-helps';
import { Sys_apiDTOHelp } from '@/api/sys-api/dto-help/sys-api-helps';
import { Sys_permissionDTOHelp } from '@/api/sys-permission/dto-help/sys-permission-helps';
import { Sys_custom_resourceDTOHelp } from '@/api/sys-custom-resource/dto-help/sys-custom-resource-helps';

/**
 * Sys_deploy_systemDTODTO辅助类
 * 基于template/src/api/{{appEntities}}/data-service/{{appEntities}}-help.ts.hbs生成
 *
 * @export
 * @class Sys_deploy_systemDTOHelp
 */
export class Sys_deploy_systemDTOHelp {
	/**
	 * 获取实体数据服务
	 *
	 * @static
	 * @param {IParam} context
	 * @return {*}  {Promise<IEntityLocalDataService<ISys_deploy_system>>}
	 * @memberof Sys_deploy_systemDTOHelp
	 */
	public static async getService(
		context: IParam
	): Promise<IEntityLocalDataService<ISys_deploy_system>> {
		return App.getDataService('Sys_deploy_system', context);
	}

	/**
	 * 获取是否缓存
	 *
	 * @memberof Sys_deploy_systemDTOHelp
	 */
	public static getEnableCache() {
		return false;
	}

	/**
	 * DTO转化成数据对象
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {IParam} source dto对象
	 * @return {*}
	 * @memberof Sys_deploy_systemDTOHelp
	 */
	public static async toDataObj(context: IContext, source: IParam) {
		const _data: any = {};
		//  系统标识
		_data.id = source.id;
		//  系统名
		_data.system_id = source.system_id;
		//  系统显示名称
		_data.display_name = source.display_name;
		//  创建人
		_data.creator = source.creator;
		//  创建时间
		_data.create_time = source.create_time;
		//  最后更新人
		_data.updater = source.updater;
		//  最后更新时间
		_data.update_time = source.update_time;
		//  DC
		_data.dc = source.dc;
		//  菜单
		_data.menus = source.menus;
		//  menus
		if (Sys_menuDTOHelp.getEnableCache()) {
			if (source && source.menus && source.menus.length > 0) {
				await Sys_menuDTOHelp.setCacheDataArray(context, source.menus);
			}
			_data.menus = source.menus;
		} else {
			_data.menus = source.menus;
		}
		//  组件
		_data.components = source.components;
		//  components
		if (Sys_componentDTOHelp.getEnableCache()) {
			if (source && source.components && source.components.length > 0) {
				await Sys_componentDTOHelp.setCacheDataArray(
					context,
					source.components
				);
			}
			_data.components = source.components;
		} else {
			_data.components = source.components;
		}
		//  接口
		_data.apis = source.apis;
		//  apis
		if (Sys_apiDTOHelp.getEnableCache()) {
			if (source && source.apis && source.apis.length > 0) {
				await Sys_apiDTOHelp.setCacheDataArray(context, source.apis);
			}
			_data.apis = source.apis;
		} else {
			_data.apis = source.apis;
		}
		//  权限资源
		_data.permissions = source.permissions;
		//  permissions
		if (Sys_permissionDTOHelp.getEnableCache()) {
			if (source && source.permissions && source.permissions.length > 0) {
				await Sys_permissionDTOHelp.setCacheDataArray(
					context,
					source.permissions
				);
			}
			_data.permissions = source.permissions;
		} else {
			_data.permissions = source.permissions;
		}
		//  自定义资源
		_data.custom_resources = source.custom_resources;
		//  custom_resources
		if (Sys_custom_resourceDTOHelp.getEnableCache()) {
			if (
				source &&
				source.custom_resources &&
				source.custom_resources.length > 0
			) {
				await Sys_custom_resourceDTOHelp.setCacheDataArray(
					context,
					source.custom_resources
				);
			}
			_data.custom_resources = source.custom_resources;
		} else {
			_data.custom_resources = source.custom_resources;
		}
		//  MD5校验
		_data.md5_checksum = source.md5_checksum;
		//  绑定标识
		_data.binding_id = source.binding_id;
		//  描述
		_data.description = source.description;
		//  是否启用标识
		_data.enabled = source.enabled;
		//  处理预置字段（以srf开头）
		if (source && Object.keys(source).length > 0) {
			Object.keys(source).forEach((key: string) => {
				if (key.startsWith('srf')) {
					_data[key] = source[key];
				}
			});
		}
		return _data;
	}

	/**
	 * 转化数据（DTO转化成数据对象）
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {IParam} data 数据对象
	 * @return {*}
	 * @memberof Sys_deploy_systemDTOHelp
	 */
	public static async toDataObjArray(context: IContext, data: any[]) {
		const _data: any[] = [];
		if (data && Array.isArray(data) && data.length > 0) {
			for (let i = 0; i < data.length; i++) {
				const tempdata = await this.toDataObj(context, data[i]);
				_data.push(tempdata);
			}
		}
		return _data;
	}

	/**
	 * 数据对象转化成DTO
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {IParam} source 数据对象
	 * @return {*}
	 * @memberof Sys_deploy_systemDTOHelp
	 */
	public static async toDto(context: IContext, source: IParam) {
		const _data: any = {};
		const excludes: string[] = [
			'menus',
			'components',
			'apis',
			'permissions',
			'custom_resources',
		];
		//  系统标识
		if (source.hasOwnProperty('id')) {
			_data.id = source.id;
		}
		//  系统名
		if (source.hasOwnProperty('system_id')) {
			_data.system_id = source.system_id;
		}
		//  系统显示名称
		if (source.hasOwnProperty('display_name')) {
			_data.display_name = source.display_name;
		}
		//  创建人
		if (source.hasOwnProperty('creator')) {
			_data.creator = source.creator;
		}
		//  创建时间
		if (source.hasOwnProperty('create_time')) {
			_data.create_time = source.create_time;
		}
		//  最后更新人
		if (source.hasOwnProperty('updater')) {
			_data.updater = source.updater;
		}
		//  最后更新时间
		if (source.hasOwnProperty('update_time')) {
			_data.update_time = source.update_time;
		}
		//  DC
		if (source.hasOwnProperty('dc')) {
			_data.dc = source.dc;
		}
		//  菜单
		if (source.hasOwnProperty('menus')) {
			_data.menus = source.menus;
		}
		//  menus
		if (source.hasOwnProperty('menus')) {
			_data.menus = source.menus;
		} else if (Sys_menuDTOHelp.getEnableCache()) {
			_data.menus = await Sys_menuDTOHelp.getCacheDataArray(context);
		}
		//  组件
		if (source.hasOwnProperty('components')) {
			_data.components = source.components;
		}
		//  components
		if (source.hasOwnProperty('components')) {
			_data.components = source.components;
		} else if (Sys_componentDTOHelp.getEnableCache()) {
			_data.components = await Sys_componentDTOHelp.getCacheDataArray(context);
		}
		//  接口
		if (source.hasOwnProperty('apis')) {
			_data.apis = source.apis;
		}
		//  apis
		if (source.hasOwnProperty('apis')) {
			_data.apis = source.apis;
		} else if (Sys_apiDTOHelp.getEnableCache()) {
			_data.apis = await Sys_apiDTOHelp.getCacheDataArray(context);
		}
		//  权限资源
		if (source.hasOwnProperty('permissions')) {
			_data.permissions = source.permissions;
		}
		//  permissions
		if (source.hasOwnProperty('permissions')) {
			_data.permissions = source.permissions;
		} else if (Sys_permissionDTOHelp.getEnableCache()) {
			_data.permissions = await Sys_permissionDTOHelp.getCacheDataArray(
				context
			);
		}
		//  自定义资源
		if (source.hasOwnProperty('custom_resources')) {
			_data.custom_resources = source.custom_resources;
		}
		//  custom_resources
		if (source.hasOwnProperty('custom_resources')) {
			_data.custom_resources = source.custom_resources;
		} else if (Sys_custom_resourceDTOHelp.getEnableCache()) {
			_data.custom_resources =
				await Sys_custom_resourceDTOHelp.getCacheDataArray(context);
		}
		//  MD5校验
		if (source.hasOwnProperty('md5_checksum')) {
			_data.md5_checksum = source.md5_checksum;
		}
		//  绑定标识
		if (source.hasOwnProperty('binding_id')) {
			_data.binding_id = source.binding_id;
		}
		//  描述
		if (source.hasOwnProperty('description')) {
			_data.description = source.description;
		}
		//  是否启用标识
		if (source.hasOwnProperty('enabled')) {
			_data.enabled = source.enabled;
		}
		// 附加额外数据
		if (source && Object.keys(source).length > 0 && keys) {
			Object.keys(source).forEach((key: string) => {
				if (!keys.includes(key) && !excludes.includes(key)) {
					_data[key] = source[key];
				}
			});
		}
		return _data;
	}

	/**
	 * 转化数组(数据对象转化成DTO数组)
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {IParam[]} data 数据对象
	 * @return {*}
	 * @memberof Sys_deploy_systemDTOHelp
	 */
	public static async toDtoArray(context: IContext, data: IParam[]) {
		const _data: any[] = [];
		if (data && Array.isArray(data) && data.length > 0) {
			for (let i = 0; i < data.length; i++) {
				const tempdata = await this.toDto(context, data[i]);
				_data.push(tempdata);
			}
		}
		return _data;
	}

	/**
	 * 处理响应DTO对象
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {IParam} data 数据对象
	 * @return {*}
	 * @memberof Sys_deploy_systemDTOHelp
	 */
	public static async set(context: IContext, data: IParam) {
		const _data: IParam = await this.toDataObj(context, data);
		return _data;
	}

	/**
	 * 处理请求数据对象
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {IParam} data 数据对象
	 * @return {*}
	 * @memberof Sys_deploy_systemDTOHelp
	 */
	public static async get(context: IContext, data: IParam = {}) {
		return await this.toDto(context, data);
	}

	/**
	 * 获取缓存数据
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {string} srfkey 数据主键
	 * @return {*}
	 * @memberof Sys_deploy_systemDTOHelp
	 */
	public static async getCacheData(context: IContext, srfkey: string) {
		return {};
	}

	/**
	 * 获取缓存数组
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @return {*}
	 * @memberof Sys_deploy_systemDTOHelp
	 */
	public static async getCacheDataArray(context: IContext) {
		return [];
	}

	/**
	 * 设置缓存数据
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {IParam} data 数据
	 * @return {*}
	 * @memberof Sys_deploy_systemDTOHelp
	 */
	public static async setCacheData(context: IContext, data: IParam) {}

	/**
	 * 设置缓存数组
	 *
	 * @static
	 * @param {IContext} context 应用上下文
	 * @param {any[]} data 数据
	 * @return {*}
	 * @memberof Sys_deploy_systemDTOHelp
	 */
	public static async setCacheDataArray(context: IContext, data: any[]) {}
}
